# RESTFUL设计风格

# 网络请求 -- 资源
#

# /v1/user/get
# /v1/user/modify
# /v1/user/delete
# /v1/user/add

# rest api -- 不建议url里面携带动词
# /v1/user      -- GET 查询、所有
#               -- POST 新增
# /v1/user/<id> -- PUT 修改
#               -- DELETE 删除
#               -- GET 查询某一个

from flask import Blueprint, request
from flask_restful import Resource, Api
from model.monitor import Monitor
from model.user import UserManager
from libs.response import generate_response
from model import db
from libs.auth import login
from libs.handler import default_error_handler

monitor_bp = Blueprint("monitorapi", __name__, url_prefix="/v1")
api = Api(monitor_bp)


# 这个类就是一类资源
class MonitorView(Resource):
    # @login
    def get(self, id=None):
        # keyword = request.args.get("keyword")
        result_list = []
        if id is not None:
            monitor = Monitor.query.get(id)
            if monitor is not None:
                result_list.append(monitor)
            else:
                return generate_response(status_code=10001, message="monitor doesn't exist")
        else:
            monitors = Monitor.query.all()
            result_list += monitors
        return generate_response(message="get monitor success", data=[dict(item) for item in result_list])
        # return "this is get"

    def post(self):
        user_id = request.json.get("user_id")
        prov = request.json.get("prov")
        isp = request.json.get("isp")
        bd = request.json.get("bd")
        action = request.json.get("action")
        # 判断user表中id字段是否存在user_id
        query_user_id = UserManager.query.get(user_id)
        if query_user_id is not None:
            monitor_info = Monitor(
                user_id=user_id,
                prov=prov,
                isp=isp,
                bd=bd,
                action=action
            )
            db.session.add(monitor_info)
            db.session.commit()
            return generate_response(message="add monitor success")
        else:
            return generate_response(status_code=10001, message="user_id doesn't exist")
        # return "this is post"

    def put(self, id):
        monitor = Monitor.query.get(id)
        if monitor is not None:
            user_id = request.json.get("user_id")
            monitor.prov = request.json.get("prov")
            monitor.isp = request.json.get("isp")
            monitor.bd = request.json.get("bd")
            monitor.action = request.json.get("action")
            # 判断user表中id字段是否存在user_id
            query_user_id = UserManager.query.get(user_id)
            if query_user_id is not None:
                monitor.user_id = user_id
                db.session.add(monitor)
                db.session.commit()
                return generate_response(message="update monitor success")
            else:
                return generate_response(status_code=10001, message="user_id doesn't exist")
        else:
            return generate_response(status_code=10001, message="monitor doesn't exist")
        # return "this is put"

    def delete(self, id):
        result = Monitor.query.get(id)
        if result is not None:
            db.session.delete(result)
            db.session.commit()
            return generate_response(message="delete monitor success")
        else:
            return generate_response(status_code=10001, message="monitor doesn't exist")

        # return "this is delete"



# 针对这一类资源，都需要使用login验证装饰器
# api.decorators=[login]
# 设置异常返回标准化，开发测试环境不建议开启  因为开启后提供的信息有限
api.handle_error = default_error_handler
# 设置路由
api.add_resource(MonitorView, "/monitor")
api.add_resource(MonitorView, "/monitor/<int:id>", endpoint="monitorviewapi")
